
%include "..\include\AddrDef.inc"

[BITS 16]
[ORG BOOT_ADDR] ;boot address
Base:
jmp FindKrnldr

OEMName                db "Clover  "
BytePerSector          dw 0x200
SectorsPerClustor      db 0x1
ReservedSectorCount    dw 0x1
NumOfFATs              db 0x2
RootDirEntries         dw 0xE0
TotalSectors           dw 0xB40
MediaType              db 0xF0
SectorsPerFAT          dw 0x09
SectorsPerTrack        dw 0x12
NumOfHeads             dw 0x02
HiddenSectors          dd 0x00
FAT32Related           dd 0x00
Ignored                dw 0x00
BootSignature          db 0x29
VolumeId               dd 0x20121228
VolumeLabel            db "CloverDisk "
FileSystemType         db "FAT12   "

FindKrnldr: ;Parsing Root Dir Entry to locate loader
	cli 
	cld
	mov ax,cs
	mov ss,ax	
	mov ds,ax
	mov sp,BOOT_ADDR
	mov [CurDevice],dl
	sti
	call ClearScreen
	
	;
	;parse FAT12
	;Root Dir Sector = ((NumOfFATs * SectorsPerFAT)+ReservedSectorCount ) 
	xor ax,ax
	mov al, byte [SectorsPerFAT]
	mul byte [NumOfFATs]
READ_ROOT_DIR:
	inc al ; add reserved sector count
	mov si,ax		
	mov ax,ES_ADDRESS
	mov es,ax
	mov ax, word [RootDirEntries] ; equal to how many sectors to read
	xor bx,bx
	call ReadSector
	jc READ_ERROR
	

	xor di,di	
PARSING_ROOT_DIR:	
	mov [TempBuffer],di
	mov ax,[es:di]	
	cmp al,0x00
	jz READ_ERROR	
	mov ax,KernelName
	mov si,ax
	mov cx,0x08
	repe cmpsb	
	jz LOAD_KERNEL	
	mov di,[TempBuffer]
	add di,0x20
	jmp PARSING_ROOT_DIR
	
LOAD_KERNEL:
	call PrintSuccess
	;mov bx,[TempPointer]
;	mov dl,[bx]
	;cmp dl,0x00h ;;end of root entry, no loader?  print error
;	jz READ_ERROR
	;cmp [dl],0xe5h
;	jz NEXT_ENTRY
	;mov dl,[TempPointer+0x0b]
;	test [dl],0x20
	;jz NEXT_ENTRY
;	mov cx,0x0bh
	;mov di,dx
;	mov si,LoaderName
	;repe cmpsb
;	jz READ_FAT_CHAIN
	;
;NEXT_ENTRY:
	;add [TempPointer],0x20h ;to next entry
;	jmp PARSING_EACH_ENTRY
;
;READ_FAT_CHAIN:
;;; xxxooo
	;call ReadSector
;	jc READ_ERROR
	;jmp START_ADDR ;;bye bye, go to new world

READ_ERROR:
	call PrintFailMsgAndHalt

;-------------------------------------------
;ReadSector: CHS read (CHS will change cx and dx)
; si: LBA sector address
; es:bx: buffer address
;-------------------------------------------
ReadSector: ;CHS read
	
	call LBA_TO_CHS
	mov ax,0x0201
	mov dl,[CurDevice]
	int 13h
	ret

;-------------------------------------------
;;LBA_TO_CHS:
;; si: LBA Sector address
;; return :
;; ch : cylinder
;; dh : head
;; cl : sector
;-------------------------------------------
LBA_TO_CHS:
	mov ax,si
	div byte [SectorsPerTrack]
	mov cl,ah ; LBA % SPT
	inc cl  ; Sector : (LBA % SPT ) + 1	
	movzx ax,al 
	div byte [NumOfHeads]
	mov ch,al ; Cylinder : LBA / SPT / HPC
	mov dh,ah ; Head : (LBA / SPT) % HPC
	ret



;-------------------------------------------
;;PrintFailMsgAndHalt:
;-------------------------------------------
PrintSuccess:
	mov ax,ds
	mov es,ax
	mov bp,SuccessMsg ;es:bp is the string address
	mov al,0x01
	mov ah,0x13
	mov cx,0x09
	mov bx,000ch
	mov dx,0000h
	int 10h
	jmp $
	ret
;-------------------------------------------
;;PrintFailMsgAndHalt:
;-------------------------------------------
PrintFailMsgAndHalt:
	
	mov ax,ds
	mov es,ax
	mov bp,ErrFail ;es:bp is the string address
	mov al,0x01
	mov ah,0x13
	mov cx,0x06
	mov bx,000ch
	mov dx,0000h
	int 10h
	jmp $
	ret

;-------------------------------------------
;;ClearScreen: background color is black, foreground color is white
;-------------------------------------------
ClearScreen:
	pusha
	mov ax,0x0600
	mov bh,00001111b
	xor cx,cx
	mov dh,24
	mov dl,79
	int 10h
	popa
	ret

SuccessMsg  db "Success!!"
ErrFail  db "Halt!!"
KernelName db "CLOVERKN"
EndOfBase:
	times (0x01fe-(EndOfBase-Base)) db 0
	dw 0xAA55
CurDevice  equ $    ;db 0	
TempBuffer equ $+1 ; dw 0